package com.uniuya.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelUtil {

    public static  void makeExcel(List<List<String>> result) {

        //第一步，创建一个workbook对应一个excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //第二部，在workbook中创建一个sheet对应excel中的sheet
        HSSFSheet sheet = workbook.createSheet("病例日期表");
        //第三部，在sheet表中添加表头第0行，老版本的poi对sheet的行列有限制
        HSSFRow row = sheet.createRow(0);
        //第四步，创建单元格，设置表头
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("date");
        cell = row.createCell(1);
        cell.setCellValue("month");
        cell = row.createCell(2);
        cell.setCellValue("weekday");
        cell = row.createCell(3);
        cell.setCellValue("number");
        cell = row.createCell(4);
        cell.setCellValue("IsCaseDay");
        cell = row.createCell(5);
        cell.setCellValue("temperature");
        cell = row.createCell(6);
        cell.setCellValue("airPressure");
        cell = row.createCell(7);
        cell.setCellValue("RH(%)");
        cell = row.createCell(8);
        cell.setCellValue("so2μg/m3");
        cell = row.createCell(9);
        cell.setCellValue("no2μg/m3");
        cell = row.createCell(10);
        cell.setCellValue("pm10μg/m3");
        cell = row.createCell(11);
        cell.setCellValue("co mg/m3");
        cell = row.createCell(12);
        cell.setCellValue("o3-8h μg/m3");
        cell = row.createCell(13);
        cell.setCellValue("pm2.5μg/m3");

        //第五步，写入数据
        for(int i=0;i<result.size();i++) {

            List<String> oneData = result.get(i);
            HSSFRow row1 = sheet.createRow(i + 1);
            for(int j=0;j<oneData.size();j++) {

                //创建单元格设值
                row1.createCell(j).setCellValue(oneData.get(j));
            }
        }

        //将文件保存到指定的位置
        try {
            FileOutputStream fos = new FileOutputStream("E:\\result.xls");
            workbook.write(fos);
            System.out.println("写入成功");
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
